package org.alexdalton.jira;

import java.util.ArrayList;

import org.xmlrpc.android.XMLRPCException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class CreateIssue extends Activity {
    JiraApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createissue);
        app = JiraApp.get();
        ArrayAdapter<String> adapter;

        ArrayList<String> list;
        final ArrayList<String> projectIds = new ArrayList<String>();
        final ArrayList<Integer> typeIds = new ArrayList<Integer>();
        final ArrayList<Integer> prioIds = new ArrayList<Integer>();
        
        final Spinner spProject = (Spinner) findViewById(R.id.project);
        list = app.getProjectsLabels(projectIds);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spProject.setAdapter(adapter);
        
        final Spinner spType = (Spinner) findViewById(R.id.type);
        list = app.getTypeLabels(typeIds);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spType.setAdapter(adapter);
        
        final Spinner spPrio = (Spinner) findViewById(R.id.priority);
        list = app.getPriorityLabels(prioIds);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spPrio.setAdapter(adapter);
        
        Button b = (Button) findViewById(R.id.submit);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                try {
                    app.createIssue(projectIds.get(spProject.getSelectedItemPosition()), prioIds.get(spPrio.getSelectedItemPosition()).toString(),
                            typeIds.get(spType.getSelectedItemPosition()).toString(), null, null,
 ((TextView) findViewById(R.id.summary)).getText().toString(),
                            ((TextView) findViewById(R.id.description)).getText().toString());
                } catch (XMLRPCException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}
